package com.gaussic.controller;

<<<<<<< HEAD
import com.gaussic.model.UsersEntity;
=======
>>>>>>> 81f7d71d129c1d0688bed3f90a40505e4639ce93
import com.gaussic.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
public class MyController {
    @Autowired
    UsersRepository usersRepository;
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String display_standard(){

        return "index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addUserPost(HttpServletRequest request) {
        // 注意此处，post请求传递过来的是一个UserEntity对象，里面包含了该用户的信息
        // 通过@ModelAttribute()注解可以获取传递过来的'user'，并创建这个对象

        // 数据库中添加一个用户，该步暂时不会刷新缓存
        //userRepository.save(userEntity);
        //System.out.println(usersEntity.getBirth());
        //System.out.println(usersEntity.getLastName());

        // 数据库中添加一个用户，并立即刷新缓存
        //usersRepository.saveAndFlush(usersEntity);
        UsersEntity usersEntity = new UsersEntity();
        usersEntity.setName(request.getParameter("name"));

        String datestr = request.getParameter("birth");
        datestr += " 00:00:00";
        Timestamp ts = Timestamp.valueOf(datestr);
        usersEntity.setBirth(ts);

        usersEntity.setPassword(request.getParameter("password"));

        usersEntity.setCiscId(1);
        usersEntity.setSignupAt(ts);
        usersEntity.setCommitteeId(1);
        usersEntity.setAddress("平乐园100号");
        usersEntity.setPhone("113131");
        usersEntity.setSex(true);
        usersEntity.setSignupStatus(true);

        usersRepository.updateUsers(request.getParameter("name"),true, "", "",
        ts,true,ts,1,1,request.getParameter("password") ,1);
        usersRepository.flush(); // 刷新缓冲区

        //usersRepository.saveAndFlush(usersEntity);
        // 重定向到用户管理页面，方法为 redirect:url
        // return "redirect:/";
        return "index";
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String display_test(@ModelAttribute("users") UsersEntity usersEntity){

        return "index";
    }

    /*public String logging(@PathVariable String id, @PathVariable String password){
        usersRepository.updateUsers(id,password);
        usersRepository.flush(); // 刷新缓冲区
        return "login";
    }*/










}
